package ui;

import java.io.Console;
import java.io.File;
import java.io.FileOutputStream;

import core.IMessenger;
import core.ISpeaker;
import core.Contact;
import core.ContactList;

public class Messenger extends Thread implements IMessenger
{
	protected ISpeaker speaker;
	Console console;
	
	public Messenger(ISpeaker speaker)
	{
		this.speaker = speaker;
		speaker.setMessenger(this);
		console = System.console();
	}
	void unknownCommand(String command)
	{
		console.printf("Unknown command: %s\n", command);
	}
	void sendMessageByIndex(int index, String message)
	{
		speaker.sendMessage(ContactList.instance().get(index), message);
	}
	void sendFileByIndex(int index, String path)
	{
		speaker.sendFile(ContactList.instance().get(index), path, null);
	}
	public void messageReceived(Contact contact, String message)
	{
		console.printf("Incoming message from contact %s:\n%s\n", contact.toString(), message);
	}
	public void fileReceived(Contact contact, String name, byte[] body)
	{
		try
		{
			File dir = new File("./received/");
			if (!dir.exists())
			{
				dir.mkdirs();
			}	
			File f = new File("./received/"+name);
			f.createNewFile();
			FileOutputStream out = new FileOutputStream(f);
			out.write(body);
			out.flush();
			console.printf("Received file %s from contact %s\n", name, contact.toString());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void run()
	{
		console.printf("Messenger started!\n");
		while (true)
		{
			try
			{
				if (console.reader().ready())
				{
					String command = console.readLine();
					String[] input = command.split(" ");
					console.printf("Received command:\n");
					for (int i = 0; i < input.length; i++)
					{
						console.printf("%s\n", input[i]);
					}
					if (input[0].contains("exit"))
					{
						console.printf("Goodbye!\n");
						break;
					}
					else if (input[0].contains("send"))
					{
						if (input[1].contains("file"))
						{
							sendFileByIndex(Integer.parseInt(input[2]), input[3]);
						}
						else if (input[1].contains("message"))
						{
							sendMessageByIndex(Integer.parseInt(input[2]), input[3]);
						}
						else
						{
							sendMessageByIndex(Integer.parseInt(input[1]), input[2]);
						}
					}
					else if (input[0].contains("show"))
					{
						if (input[1].contains("contacts"))
						{
							for (int i = 0; i < ContactList.instance().getCount(); i++)
							{
								console.printf("%2d: %s\n", i, ContactList.instance().get(i).toString());
							}
						}
						else
						{
							unknownCommand(command);
						}
					}
					else if (input[0].contains("add"))
					{
						ContactList.instance().add(input[1], Integer.parseInt(input[2]));
					}
					else
					{
						if (command != "")
							unknownCommand(command);
					}
				}
				else
				{
					Thread.sleep(500);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}